<?php
/**
 * 装备中心
 */

use think\facade\Db;

if(!session_id()) session_start();


class Api_Equipment extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'index'    => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			),
			'setliang' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'liangid' => array('name' => 'liangid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '靓号ID'),
				'state'   => array('name' => 'state', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '启用状态 1-启用 0-不用'),
			),
			'setcar'   => array(
				'uid'    => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'  => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'carid'  => array('name' => 'carid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '坐骑ID'),
				'status' => array('name' => 'status', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '启用状态 1-启用 0-不用'),
			),
			'buyliang' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'liangid' => array('name' => 'liangid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '靓号ID'),
				'type'    => array('name' => 'type', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '购买方式 1-积分购买 0-余额购买'),
			),
			'buycar'   => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'carid' => array('name' => 'carid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '坐骑ID'),
				'type'  => array('name' => 'type', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '购买方式 1-积分购买 0-余额购买'),
			),
			'buyvip'   => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'vipid' => array('name' => 'vipid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => 'vip的ID'),
				'type'  => array('name' => 'type', 'type' => 'int', 'min' => 0, 'require' => true, 'desc' => '购买方式 1-积分购买 0-余额购买'),
			),
			'vip'      => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			),
			'getliang' => array(
				'p'    => array('name' => 'p', 'type' => 'int', 'min' => 1, 'default' => 1, 'require' => true, 'desc' => '页数'),
				'pnum' => array('name' => 'pnum', 'type' => 'int', 'min' => 1, 'default' => 20, 'desc' => '每页数量'),
			),
		);
	}
	
	public function index()
	{
		$uid        = (int)checkNull($this->uid);
		$checkToken = checkToken($uid, $this->token);
		if($checkToken == 700){
			//return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		/* 靓号信息 */
		$configpub  = getConfigPub();
		$liang_user = DI()->notorm->liang->where("uid=?", $uid)->order("buytime desc")->fetchAll();
		$liang_list = DI()->notorm->liang->where("status=?", 0)->limit(1, 20)->order("list_order desc")->fetchAll();
		foreach($liang_list as $k => $v){
			$liang_list[$k]['coin_date']  = number_format($v['coin']).$configpub['name_coin'];
			$liang_list[$k]['score_date'] = number_format($v['score']).$configpub['name_score'];
		}
		
		/* 坐骑信息 */
		$car_key  = 'carinfo';
		$car_list = getcaches($car_key);
		$carlist2 = [];
		if(!$car_list){
			$car_list = DI()->notorm->car->order("list_order asc")->fetchAll();
			if($car_list){
				setcaches($car_key, $car_list);
			}
		}
		foreach($car_list as $k => $v){
			$v['thumb'] = get_upload_path($v['thumb']);
			$v['swf']   = get_upload_path($v['swf']);
			// 组合
			$carlist2[$v['id']] = $v;
		}
		
		/* 用户坐骑 */
		$nowtime      = time();
		$user_carlist = DI()->notorm->car_user->where('uid=? and endtime>?', $uid, $nowtime)->fetchAll();
		foreach($user_carlist as $k => $v){
			if(isset($carlist2[$v['carid']])){
				$user_carlist[$k]['carinfo']      = $carlist2[$v['carid']];
				$user_carlist[$k]['endtime_date'] = date("Y-m-d", $v['endtime']);
				unset($carlist2[$v['carid']]);
			} else{
				unset($user_carlist[$k]);
			}
		}
		
		$info = ["user_carlist" => array_values($user_carlist), "carlist" => array_values($carlist2), "liang_list" => $liang_list, "liang_user" => $liang_user,];
		
		return $this->success($info, '装备获取成功!');
	}
	
	/* 设置靓号 */
	public function setliang()
	{
		$uid     = (int)checkNull($this->uid);
		$liangid = (int)checkNull($this->liangid);
		$state   = (int)checkNull($this->state);
		
		$checkToken = checkToken($uid, $this->token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		// 查询
		$setstatus = $state ? 0 : 1;
		$setstatus = $state ? 1 : 0;
		$isexist   = DI()->notorm->liang->where('uid=? and id=?', $uid, $liangid)->fetchOne();
		if(empty($isexist)){
			return $this->fail('信息错误!', 1001);
		}
		// 设置其他不能用
		DI()->notorm->liang->where("uid=?", $uid)->update(array('state' => 0));
		// 更新当前为能用
		$data = array('state' => $setstatus,);
		$list = DI()->notorm->liang->where('uid=? and id=?', $uid, $liangid)->update($data);
		// 设置用户装备
		$goodnum = $isexist['name'];
		$key     = 'liang_'.$uid;
		if($setstatus == 1){
			DI()->notorm->user->where("id=?", $uid)->update(['goodnum' => $goodnum]);
			$isexist = DI()->notorm->liang->where('uid=? and id=? and state=1', $uid, $liangid)->fetchOne();
			if($isexist){
				setcaches($key, $isexist);
			}
		} else{
			DI()->notorm->user->where("id=?", $uid)->update(['goodnum' => 0]);
			delcache($key);
		}
		
		return $this->success([], '更换成功!');
	}
	
	/* 装备坐骑 */
	public function setcar()
	{
		$uid    = (int)checkNull($this->uid);
		$carid  = (int)checkNull($this->carid);
		$status = (int)checkNull($this->status);
		
		$checkToken = checkToken($uid, $this->token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		// 查询
		$setstatus = $status ? 0 : 1;
		$setstatus = $status ? 1 : 0;
		$isexist   = DI()->notorm->car_user->where('uid=? and carid=?', $uid, $carid)->fetchOne();
		if(empty($isexist)){
			return $this->fail('信息错误!', 1001);
		}
		
		// 设置其他不能用
		$data1 = array('status' => 0,);
		DI()->notorm->car_user->where("uid=?", $uid)->update($data1);
		// 更新当前为能用
		$data   = array('status' => $setstatus,);
		$result = DI()->notorm->car_user->where('uid=? and carid=?', $uid, $carid)->update($data);
		// 设置用户装备
		$key = 'car_'.$uid;
		if($setstatus){
			$isexist = DI()->notorm->car_user->where('uid=? and status=1', $uid)->fetchOne();
			if($isexist){
				setcaches($key, $isexist);
			}
		} else{
			delcache($key);
		}
		
		return $this->success([], '更换成功!');
	}
	
	/* 购买靓号 */
	public function buyliang()
	{
		$uid     = (int)checkNull($this->uid);
		$liangid = (int)checkNull($this->liangid);
		$type    = (int)checkNull($this->type);
		
		$checkToken = checkToken($uid, $this->token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		// 查询装备信息
		$lianginfo = DI()->notorm->liang->where("id=?", $liangid)->fetchOne();
		if(empty($lianginfo)){
			return $this->fail('靓号信息错误!', 1001);
		}
		
		if($lianginfo['status'] == 1){
			$rs['code'] = 1003;
			$rs['msg']  = '该靓号已出售';
			echo json_encode($rs);
			
			return;
		}
		if($lianginfo['status'] == 2){
			$rs['code'] = 1003;
			$rs['msg']  = '该靓号已下架';
			echo json_encode($rs);
			
			return;
		}
		
		$total     = $lianginfo['coin'];
		$score     = $lianginfo['score'];
		$giftid    = $lianginfo['id'];
		$addtime   = time();
		$giftcount = 1;
		
		if($type == 1){
			/* 积分 */
			$ifok = DI()->notorm->user->where("id=? and score>=?", $uid, $score)->update(array("score" => new NotORM_Literal("score - {$score}"),));
			if(!$ifok){
				return $this->fail('积分不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '18',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $score,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_scorerecord->insert($insert);
		} else{
			/* 更新用户余额 消费 */
			$update = array("coin" => new NotORM_Literal("coin - {$total}"), "consumption" => new NotORM_Literal("consumption + {$total}"),);
			$ifok   = DI()->notorm->user->where("id=? and coin>=?", $uid, $total)->update($update);
			if(!$ifok){
				return $this->fail('余额不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '18',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $total,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_coinrecord->insert($insert);
		}
		
		// 更新购买状态
		$data = array('uid' => $uid, 'status' => 1, 'buytime' => $addtime,);
		DI()->notorm->liang->where("id=?", $liangid)->update($data);
		
		$userinfo = DI()->notorm->user->select("coin,score")->where("id=?", $uid)->fetchOne();
		
		return $this->success($userinfo, '您已成功购买'.$lianginfo['name'].'坐骑，请前往“装备中心”进行查看！');
	}
	
	/* 购买坐骑 */
	public function buycar()
	{
		$uid   = (int)checkNull($this->uid);
		$carid = (int)checkNull($this->carid);
		$type  = (int)checkNull($this->type);
		
		$checkToken = checkToken($uid, $this->token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		// 查询装备信息
		$carinfo = DI()->notorm->car->where("id=?", $carid)->fetchOne();
		if(empty($carinfo)){
			return $this->fail('坐骑信息错误!', 1001);
		}
		
		$total     = $carinfo['needcoin'];
		$score     = $carinfo['score'];
		$giftid    = $carinfo['id'];
		$addtime   = time();
		$giftcount = 1;
		if($type == 1){
			/* 积分 */
			$ifok = DI()->notorm->user->where("id=? and score>=?", $uid, $score)->update(array("score" => new NotORM_Literal("score - {$score}"),));
			if(!$ifok){
				return $this->fail('积分不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '5',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $score,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_scorerecord->insert($insert);
		} else{
			/* 更新用户余额 消费 */
			$update = array("coin" => new NotORM_Literal("coin - {$total}"), "consumption" => new NotORM_Literal("consumption + {$total}"),);
			$ifok   = DI()->notorm->user->where("id=? and coin>=?", $uid, $total)->update($update);
			if(!$ifok){
				return $this->fail('余额不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '5',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $total,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_coinrecord->insert($insert);
		}
		
		// 更新装备信息
		$endtime = $addtime + 60 * 60 * 24 * 30 * $giftcount;
		$usercar = DI()->notorm->car_user->where("uid=? and carid=?", $uid, $carid)->fetchOne();
		if($usercar){
			if($usercar['endtime'] > $addtime){
				$endtime = $usercar['endtime'] + 60 * 60 * 24 * 30 * $giftcount;
			}
			$data = array('endtime' => $endtime,);
			DI()->notorm->car_user->where("id=?", $usercar['id'])->update($data);
		} else{
			$data = array('uid' => $uid, 'addtime' => $addtime, 'endtime' => $endtime, 'carid' => $carid,);
			DI()->notorm->car_user->insert($data);
		}
		
		$userinfo = DI()->notorm->user->select("coin,score")->where("id=?", $uid)->fetchOne();
		
		
		return $this->success($userinfo, '您已成功购买'.$carinfo['name'].'坐骑，请前往“装备中心”进行查看！');
	}
	
	/* 购买VIP */
	function buyvip()
	{
		$uid   = (int)checkNull($this->uid);
		$vipid = (int)checkNull($this->vipid);
		$type  = (int)checkNull($this->type);
		
		if(checkToken($uid, $this->token) == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		}
		
		$vipinfo = DI()->notorm->vip->where("id=?", $vipid)->fetchOne();
		if(empty($vipinfo)){
			return $this->fail('VIP信息错误!', 1001);
		}
		
		$total     = $vipinfo['coin'];
		$score     = $vipinfo['score'];
		$giftid    = $vipinfo['id'];
		$addtime   = time();
		$giftcount = $vipinfo['length'];
		
		if($type == 1){
			/* 积分 */
			$ifok = DI()->notorm->user->where("id=? and score>=?", $uid, $score)->update(array("score" => new NotORM_Literal("score - {$score}"),));
			if(!$ifok){
				return $this->fail('积分不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '4',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $score,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_scorerecord->insert($insert);
		} else{
			/* 更新用户余额 消费 */
			$update = array("coin" => new NotORM_Literal("coin - {$total}"), "consumption" => new NotORM_Literal("consumption + {$total}"),);
			$ifok   = DI()->notorm->user->where("id=? and coin>=?", $uid, $total)->update($update);
			if(!$ifok){
				return $this->fail('余额不足!', 1002);
			}
			
			/* 添加记录 */
			$insert = array(
				"type"      => '0',
				"action"    => '4',
				"uid"       => $uid,
				"touid"     => $uid,
				"giftid"    => $giftid,
				"giftcount" => $giftcount,
				"totalcoin" => $total,
				"addtime"   => $addtime,
			);
			DI()->notorm->user_coinrecord->insert($insert);
		}
		
		// 更新装备信息
		$endtime = $addtime + 60 * 60 * 24 * 30 * $giftcount;
		$uservip = DI()->notorm->vip_user->where("uid=?", $uid)->fetchOne();
		if($uservip){
			if($uservip['endtime'] > $addtime){
				$endtime = $uservip['endtime'] + 60 * 60 * 24 * 30 * $giftcount;
			}
			$data = array('endtime' => $endtime,);
			DI()->notorm->vip_user->where("uid=?", $uid)->update($data);
		} else{
			$data = array('uid' => $uid, 'addtime' => $addtime, 'endtime' => $endtime,);
			DI()->notorm->vip_user->insert($data);
		}
		
		
		// 更新购买状态
		$result  = date("Y.m.d", $endtime);
		$key     = 'vip_'.$uid;
		$isexist = DI()->notorm->vip_user->where("uid=?", $uid)->fetchOne();
		if($isexist){
			setcaches($key, $isexist);
		}
		
		$userinfo = DI()->notorm->user->select("coin,score")->where("id=?", $uid)->fetchOne();
		// 组合
		$userinfo['endtime'] = $result;
		
		return $this->success($userinfo, '您已成功购买vip，请前往“vip中心”进行查看！');
	}
	
	// vip会员中心
	public function vip()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$nowtime    = time();
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$long     = array('1' => '1个月', '3' => '3个月', '6' => '6个月', '12' => '12个月',);
		$user     = DI()->notorm->user->select("id,user_nickname,coin,score")->where("id=?", $uid)->fetchOne();
		$vip_list = DI()->notorm->vip->order("list_order asc")->fetchAll();
		$uservip  = DI()->notorm->vip_user->where('uid=? and endtime>?', $uid, $nowtime)->fetchOne();  // 用户VIP
		if($uservip){
			$uservip['vip_txt'] = '续费';
			$uservip['endtime'] = date("Y.m.d", $uservip['endtime']);
		} else{
			$uservip['endtime'] = '';
			$uservip['vip_txt'] = '开通';
		}
		
		// 靓号
		/*$configpub  = getConfigPub();
		$liang_list = DI()->notorm->liang->where("status=0")->order("list_order asc,id desc")->limit(21)->fetchAll();
		foreach($liang_list as $k => $v){
			$liang_list[$k]['coin_date']  = number_format($v['coin']).$configpub['name_coin'];
			$liang_list[$k]['score_date'] = number_format($v['score']).$configpub['name_score'];
		}*/
		
		//  坐骑
		$car_key  = 'carinfo';
		$car_list = getcaches($car_key);
		if(!$car_list){
			$car_list = DI()->notorm->car->order("list_order asc")->fetchAll();
			if($car_list){
				setcaches($car_key, $car_list);
			}
		}
		foreach($car_list as $k => $v){
			$car_list[$k]['thumb'] = get_upload_path($v['thumb']);
			$car_list[$k]['swf']   = get_upload_path($v['swf']);
		}
		// 'liang_list' => $liang_list
		$result = ['car_list' => $car_list, 'uservip' => $uservip, 'vip_list' => $vip_list, 'user' => $user, 'long' => $long];
		
		return $this->success($result);
	}
	
	/* 靓号加载更多 */
	function getliang()
	{
		$p    = checkNull($this->p);
		$pnum = checkNull($this->pnum);
		if(!$p){
			$p = 1;
		}
		if($pnum < 1){
			$pnum = 20;
		}
		
		$isscroll   = 1;
		$configpub  = getConfigPub();
		$start      = ($p - 1) * $pnum;
		$liang_list = DI()->notorm->liang->where("status=?", 0)->limit($start, $pnum)->order("list_order desc")->fetchAll();
		foreach($liang_list as $k => $v){
			$liang_list[$k]['coin_date']  = number_format($v['coin']).$configpub['name_coin'];
			$liang_list[$k]['score_date'] = number_format($v['score']).$configpub['name_score'];
		}
		
		$list_num = count($liang_list);
		if($list_num < $pnum){
			$isscroll = 0;
		}
		
		$info['list']     = $liang_list;
		$info['nums']     = $list_num;
		$info['isscroll'] = $isscroll;
		
		return $this->success($info);
	}
}
